package com.wcy.bysj.service.impl;

import com.wcy.bysj.common.*;
import com.wcy.bysj.dao.ClassMapper;
import com.wcy.bysj.dao.FoodMapper;
import com.wcy.bysj.dao.UserMapper;
import com.wcy.bysj.entity.Food;
import com.wcy.bysj.entity.User;
import com.wcy.bysj.entity.request.FoodModel;
import com.wcy.bysj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private ClassMapper classMapper;

    @Override
    public User login(User user) {
        if (EmptyUtil.isEmpty(user)) {
            throw new MyException(ResultEnum.ERROR.getCode(),"传参为空");
        }
        User user1 = userMapper.selectUserByUser(user);
        String hobby = userMapper.selectHobbyById(user1);
        List<String> list = classMapper.selectClassByHobbyStr(Arrays.asList(hobby.split(",")));
        user1.setHobby(String.join(",",list));
        return user1;
    }

    @Override
    public Result regist(User user) {
        if (EmptyUtil.isEmpty(user)) {
            return Result.error(ResultEnum.ERROR.getCode(),"传参为空");
        }
        int i = userMapper.regist(user);
        return Result.success(i);
    }

    @Override
    public Page getUserList(User user) {
        Page page = new Page();
        BigInteger total = userMapper.selectUserCount(user);
        user.setTotal(total);
        List<User> userList = userMapper.selectUserList(user);
        userList.stream().forEach(
                item -> {
                    String hobby = userMapper.selectHobbyById(item);
                    List<String> list = classMapper.selectClassByHobbyStr(Arrays.asList(hobby.split(",")));
                    String str = String.join(",",list);
                    item.setHobby(str);
                }
        );
        page.setTotal(total);
        page.setTotalPages(user.getTotalPages());
        page.setData(userList);
        return page;
    }

    @Override
    public Page selectUserByName(User user) {
        Page page = new Page();
        BigInteger total = userMapper.selectUserCount(user);
        user.setTotal(total);
        List<User> userList = userMapper.selectUserListByName(user);
        userList.stream().forEach(item -> {
            if (EmptyUtil.isEmpty(item.getPhone())) {
                item.setPhone("space");
            }
        });
        page.setTotal(total);
        page.setTotalPages(user.getTotalPages());
        page.setData(userList);
        return page;
    }

    @Override
    public Result deleteUser(User user) {
        if (EmptyUtil.isEmpty(user) || EmptyUtil.isEmpty(user.getId())) {
            return Result.error(ResultEnum.ERROR.getCode(),"没传参");
        }
        int i = userMapper.deleteUser(user);
        return Result.success("成功");
    }

    @Override
    public Result updateUser(User user) {
        if (EmptyUtil.isEmpty(user) || EmptyUtil.isEmpty(user.getId())) {
            return Result.error(ResultEnum.ERROR.getCode(),"没传参");
        }
        int i = userMapper.updateUser(user);
        return Result.success("成功");
    }

    @Override
    public Result addUser(User user) {
        if (EmptyUtil.isEmpty(user)) {
            return Result.error(ResultEnum.ERROR.getCode(),"传参为空");
        }

        int i = userMapper.regist(user);
        return Result.success(i);
    }

    @Override
    public Result getUserFood(User user) {
        if (EmptyUtil.isEmpty(user)) {
            return Result.error(ResultEnum.ERROR.getCode(),"传参为空");
        }
        String hobby = userMapper.selectHobbyById(user);
        if(EmptyUtil.isEmpty(hobby)){
            return null;
        }

        List<Food> foodListByIds
                = foodMapper.getFoodListByIds(Arrays.asList(hobby.split(",")));


        Map<String, List<Food>> map
                = foodListByIds.stream().collect(Collectors.groupingBy(Food::getSmell));

        List<FoodModel> returnList = new ArrayList<>();

        for (String s : map.keySet()) {
            FoodModel foodModel = new FoodModel();
            foodModel.setSmell(s);
            foodModel.setFoods(map.get(s));
            returnList.add(foodModel);
        }
        user.setFoodModelList(returnList);
        return Result.success(user);
    }
}
